package com.wh.book.service;

import com.wh.book.enums.BookStatus;
import com.wh.book.mapper.BookMapper;
import com.wh.book.model.BookInfo;
import com.wh.book.model.pageRequest;
import com.wh.book.model.pageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public pageResult getBookListByPage(pageRequest pageRequest) {
        Integer count = bookMapper.getCount();
        List<BookInfo> bookList = bookMapper.queryBookListByPage
                (pageRequest.getOffset(), pageRequest.getPageSize());
        for (BookInfo book : bookList) {
            book.setStatusCN(BookStatus.getDescByCode(book.getStatus()).getDesc());
        }
        return new pageResult(bookList, count);
    }


    public int insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public int deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return updateBookById(bookInfo);
    }

    public int updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public int batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
